package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.MaterialClassificationBean;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.RecyclerView.Adapter;
import static android.support.v7.widget.RecyclerView.ViewHolder;
import static android.view.View.OnClickListener;

public class CheckGradeDialogAdapter extends Adapter {

    private Context mContext;
    private LayoutInflater factory;
    private List<MaterialClassificationBean> list = new ArrayList<>();
    private int selectPosition = 0;

    public CheckGradeDialogAdapter(Context context) {
        if (context == null) new Throwable("context 空啦！！");
        this.mContext = context;
        factory = LayoutInflater.from(context);
    }

    public void setData(List<MaterialClassificationBean> list) {
        this.list = list;
//        selectPosition=0;//默认选中第一个条目
        notifyDataSetChanged();
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {

        void onItemClick(ViewHolder holder, List<MaterialClassificationBean> list, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        View inflate = factory.inflate(R.layout.item_dialog_grade, parent, false);
        viewHolder = new CalatogViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final TextView textView = (TextView) holder.itemView;
        final MaterialClassificationBean gradeCheckBean = list.get(position);

        if (gradeCheckBean.isSelect()){
            selectPosition=position;
//        if (position == selectPosition) {
            gradeCheckBean.setSelect(true);
            textView.setText("· " + gradeCheckBean.getGradeName_plus());
        } else {
            textView.setText(gradeCheckBean.getGradeName_plus());
        }

        textView.setSelected(gradeCheckBean.isSelect());
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(selectPosition).setSelect(false);
                notifyItemChanged(selectPosition);
//                selectPosition = position;
                gradeCheckBean.setSelect(true);
                textView.setText("· " + gradeCheckBean.getGradeName_plus());
                textView.setSelected(gradeCheckBean.isSelect());
                notifyItemChanged(position);
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(holder, list, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class CalatogViewHolder extends ViewHolder {

        public CalatogViewHolder(View itemView) {
            super(itemView);
        }
    }
}
